package com.itheima.controller;

import com.itheima.Log.Log;
import com.itheima.common.R;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

@RestController
public class DeptController {
    @Autowired
    DeptService deptService;

    @GetMapping("/depts")
    //查询所有部门没有请求参数
    public R list() {
        List<Dept> list = deptService.list();
        return R.ok(list);
    }

    @PostMapping("/depts")
    @Log
    public R add(@RequestBody Dept dept) {
        deptService.add(dept);
        return R.ok();
    }

    @DeleteMapping("/depts")
    @Log
    public R delete(Integer id) throws FileNotFoundException {
        deptService.delete(id);
        return R.ok();
    }

    @GetMapping("/depts/{id}")
    public R getById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        return R.ok(dept);
    }

    @PutMapping("/depts")
    @Log
    public R update(@RequestBody Dept dept) {
        deptService.update(dept);
        return R.ok();
    }

}
